"""
@name : ip_time_带宽
@author : chenwenying
@projectname: tree.py
@time : 2022/4/16
"""

import requests
import json
import time
import calendar

with open('E:\代码\weblog.txt', encoding='UTF-8') as fp:
    with open('record.txt', 'w', encoding='UTF-8') as fp1:
        for i in fp:
            # 获取ip地址
            ip = i.split()[0]
            # 得到这个网页的内容
            url1 = requests.get(url='https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=%s'%ip)
            # 把json格式的字符串转化成对应的数据结构对象
            url2 = json.loads(url1.text)
            # 得到省份
            province1 = url2['data']['region']
            # 得到运营商
            isp1 = url2['data']['isp']

            # 得到数据中的时间
            time1 = i.split()[3]
            for j in '[/:':
                # 消去数据中的非数字字符
                time2 = time1.replace(j, ' ')
                time1 = time2
            # 得到时间列表['04', 'Jun', '2021', '04', '00', '20']
            time3 = time2.strip().split(' ')
            # 得到小时、分钟、秒的列表['05', '16', '45']
            e = time3[3:]
            # 得到标准时间2021-6-04 04:00:20
            time4 = f'{time3[2]}-{list(calendar.month_abbr).index(time3[1])}-{time3[0]} {":".join(e)}'
            result1 = time4 + ' ' + province1 + ' ' + isp1 + ' ' + i.split()[9]
            fp1.write(result1 + '\n')


















